﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CalculateModelClasses;
using RayTracingModel;
//using RayCalInfo;
namespace FileOutput
{
    public class PowerOutput : P2mFileOutput
    {

        public static void OutPath(List<List<CalculateModelClasses.Path>> Paths, string sPath, string projectName)//需要提供一个接受区域所有路径的信息
        {
            //sPath += projectName;
            string Rxname = Paths[0][0].node[Paths[0][0].node.Count - 1].name;
            string groupnum = Paths[0][0].node[Paths[0][0].node.Count - 1].num.ToString();
            if (Paths[0][0].node[Paths[0][0].node.Count - 1].num < 10)
            {
                groupnum = "0" + groupnum;
            }
            FileStream fs = new FileStream(sPath + projectName + ".power" + ".t001_0" + Paths[0][0].Txnum + ".r0" + groupnum + ".p2m", FileMode.Create);//如果是接收区域的话，多个接收点组成一组接收机组成一个编号
            StreamWriter sw = new StreamWriter(fs);
            List<PathNode> RxNode = new List<PathNode>();
            for (int i = 0; i < Paths.Count; i++)
            {
                RxNode.Add(Paths[i][0].node[Paths[i][0].node.Count - 1]);
            }
            foreach (List<CalculateModelClasses.Path> path in Paths)
            {
                double distance = Intersection.Distance(path[0].node[path[0].node.Count - 1].position, path[0].node[0].position);
                Plural temppower = GetTolPower(path);
                OutEveryRxPath(RxNode, Rxname, temppower, distance);

            }
            sw.Write(sb);
            sb.Clear();
            sw.Close();
            fs.Close();
        }
        public static void OutPath1(List<List<CalculateModelClasses.Path>> Paths, string sPath)
        {
            FileStream fs = new FileStream(sPath + @"PowerOutputNull.p2m", FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            sw.Close();
            fs.Close();
        }

        private static void OutEveryRxPath(List<PathNode> RxNode, string Rxname, Plural p, double distance)//接收机的名字,加到path里面
        {
            sb.AppendLine("# Receiver Set:" + Rxname);
            sb.AppendLine("#   Rx#      X(m)           Y(m)          Z(m)     Distance (m) Power (dBm)  Phase (Deg.)");
            double angle = Math.Atan(p.Im / p.Re) * 180 / Math.PI;
            if (angle < 0)
            {
                angle = angle + 180;
            }
            for (int i = 0; i < RxNode.Count; i++)
            {
                //Console.WriteLine((10 * Math.Log10(Math.Sqrt(Math.Pow(p.Re, 2) + Math.Pow(p.Im, 2)))));
                sb.AppendLine("     " + (i + 1) + "  " + RxNode[i].position.x.ToString("0.#######E+00") + "  " + RxNode[i].position.y.ToString("0.#######E+00") + "     " + RxNode[i].position.z.ToString("f3") + "    " + distance.ToString("f2") + "       " + ((10 * Math.Log10(Math.Sqrt(Math.Pow(p.Re, 2) + Math.Pow(p.Im, 2)))) + 30).ToString("f2") + "       " + angle.ToString("f2"));//distance需要调用两点距离公式计算出来。
            }


        }
    }
}
